<?php
include "Array2XML.php";

$filename = "/home/x/workspace/alb/www/index.php";
$ini_file = isset($argv[1])?$argv[1]:"parser.ini";
$dirname = isset($argv[2])?$argv[2]:"/home/x/workspace/alb/www";
$out_file_name = isset($argv[3])?$argv[3]:"result.xml";

$len_dirname = strlen($dirname);

$exclude_pattern = array(
	"<\/dump.*>"
	,"<\/test.*>"
	,"<\/tiny_mce.*>"
	,"<\/_admin.*>"
	,"<\/_symp.*>"
	,"<.*[0-9].*>"
	);
	
$exclude_link_pattern = array(
	"<\/dump.*>"
	,"<\/test.*>"
	,"<\/tiny_mce.*>"
	,"<\/_admin.*>"
		,"<\/_symp.*>"
		,"<.*FirePHP.*>"
		,"<.*session.php.*>"
		,"<.*lang.php.*>"
		,"<.*empty_page.php.*>"
	);
	
$exclude_path = array(
	"/dump"
	,"/test"
	,"/tiny_mce"
	,"/_admin"
	,"/_symp"
	);
	
$exclude_link_path = array(
	"/dump"
	,"/test"
	,"/tiny_mce"
	,"/_admin"
	,"/_symp"
	);


$converter = new Array2XML($out_file_name);
$data = scan_dir($dirname);
$xmlStr = $converter->convert($data);
//print $xmlStr;
print "down.\n";

	function scan_dir($dirname)
	{
		global $exclude_path;
		// Открываем текущую директорию 
		$dir = opendir($dirname);
		//	$xml_dirs = "";
		$xml_dirs = array();
		while (($file = readdir($dir)) !== false){
			if(filterFiles($dirname."/".$file)) {
				if($file != "." && $file != ".."){

					if(is_file($dirname."/".$file) && fnmatch("*.php",$file))	{

						$file_xml = parse_file($file, $dirname);
						if(!empty($file_xml)) {
							$xml_dirs = array_merge($xml_dirs, array($file_xml));
						}

					} else 	if(is_dir($dirname."/".$file)) {
						$r = scan_dir($dirname."/".$file,$fd,$fb);
						if(!empty($r)) {
							$xml_dirs = array_merge($xml_dirs , array($r));

						}
					}
				}
			}
		}
		// Закрываем директорию 
		closedir($dir);
		//	$xml_dirs = "1";
		if(!empty($xml_dirs)) {
			$xml = array("dir" => array(
		"name" => getRelativeName($dirname)
			,$xml_dirs));
		}

		return $xml;
	}

	function filterFiles($fileName) {
		global $exclude_pattern;
		return filter($fileName,$exclude_pattern);
	}

	function filterLink($fileName) {
		global $exclude_link_pattern;
		return filter($fileName,$exclude_link_pattern);
	}


	function filter($str, $exclude_arr) {

		$result = true;
		foreach ($exclude_arr as $pattern) {
			if(preg_match($pattern, $str)) {
				$result = false;
				break;
			}
		}
		return $result;
		//		return ! in_array(getRelativeName($dirname."/".$file), $exclude_path)) {
	}

	function getRelativeName($file_name) {
		global $len_dirname;
		return substr($file_name, $len_dirname);
	}

	function parse_file($filename, $path) {
		$full_name = "$path/$filename";
		//	print "parse file $full_name\n";
		$file=fopen($full_name,'r');
		$lineNum = 0;
		$link_arr = array();

		while($str=fgets($file,1024))
		{
			$lineNum++;
			$r = getFullFileName($str);
			if(!empty($r)) {
				$link = array("link" => array ("line" => $lineNum, "str" => $str, $r));
				$link_arr =array_merge($link_arr, array($link));
			}

		}
		if(!empty($link_arr)) {
			$xml = array("file" => array("name" => $filename
			,"path" => getRelativeName($path)
			,"fullName" => getRelativeName($full_name)
			,"links" => $link_arr
			));
		}
		fclose($file);

		return $xml;

	}

	function getFullFileName($str) {
		global $exclude_link_path;
		$match = array();
		$path_idx = 1;
		$name_idx = 3;
		$frmFile = "<([\/.A-Za-z0-9_-]*)(\/)([.A-Za-z0-9_-]*[.]php)>";
		if(preg_match($frmFile, $str, $match)) {
			$path = $match[$path_idx];
			$name = $match[$name_idx];
			$fullName = "$path/$name";
			if(filterLink($fullName)) {
				$xml = array("file" => array("name" => $name
				,"path" => $path
				,"fullName" => $fullName));
			}
		}
		return $xml;
	}

	/**
	 *
	 * @param unknown_type $str
	 * @deprecated
	 */
	function getFunc($str) {
		$func_req = "require";
		$func_inc = "include";
		$fname_id = 2;
		$str_id = 5;
		$m = array();
		$sm = array();
		$func_name = array($func_req, $func_inc);

		foreach ($func_name as $fn) {
			$func_format = "(.*)($fn)(.*)(\()(.*)(\))";
			$m = null;
			ereg($func_format, $str, $m);
			if(!empty($m)) {
				if(!empty($m[$fname_id])) {
					//				print "$lineNum: $m[$fname_id] = $m[$str_id]\n";
					getFile($m[$str_id])."\n";
					//	 			print
					//				$s = $m[$str_id];

					//				ereg($frmFile, $s, $sm);
					//	foreach ($m as $k) 	print "--> $k\n";
					//				print $sa[0];
					//				$sm = null;
					continue;
				}

				//			foreach ($m as $k) 	print "--> $k\n";
			}

		}
		$str = null;
	}

	function saveStrToFile($fileName, $str) {

	}

	?>
